package com.wuage.infrastructure.utils;

import com.obs.services.ObsClient;
import com.obs.services.model.*;
import com.wuage.domain.model.UacAccount;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.security.SecureRandom;

/**
 * @author Shynin
 * @version 1.0
 * @date 2020-08-20 16:18
 */
@Slf4j
public class GeneratorUtils {
    public static final Long MAX_ID = 9223372036854775807L;
    private static SecureRandom ID_RANDOM;
//    private static final byte[] SEEDS;
    private static final String STRING_CODE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final String NUMBER_CODE = "0123456789";

    static {
//        try {
//            ID_RANDOM = SecureRandom.getInstance("SHA1PRNG");
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//            ID_RANDOM = new SecureRandom();
//        }
        ID_RANDOM = new SecureRandom();
//        SEEDS = ID_RANDOM.generateSeed(Integer.MAX_VALUE >> 6);
//        ID_RANDOM.setSeed(SEEDS);
    }

    /**
     * 生成一个随机的ID 0-19位
     * @return Id
     */
    public static Long generateRandomId() {
        return Math.abs(ID_RANDOM.nextLong());
    }

    /**
     * 生成一个随机字符串
     * @param length 字符串长度
     * @return 随机字符串
     */
    public static String generateCode(Integer length) {
        StringBuilder stringBuffer = new StringBuilder();
        for(int index = 0; index < length; index++){
            int number = ID_RANDOM.nextInt(62);
            stringBuffer.append(STRING_CODE.charAt(number));
        }
        return stringBuffer.toString();
    }

    public static String generateNumberString(Integer length) {
        StringBuilder stringBuffer = new StringBuilder();
        for(int index = 0; index < length; index++){
            int number = ID_RANDOM.nextInt(10);
            stringBuffer.append(NUMBER_CODE.charAt(number));
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) throws IOException {
        String endPoint = "https://obs.cn-east-3.myhuaweicloud.com";
        String ak = "WVPHLXS2YUVQYZISZPWI";
        String sk = "uDFBYm8GKX4kHgMlPu0M6eDRuhuvFv4SrkAqjOI1";
        String bucketName = "yunjidev";
        // 创建ObsClient实例
        ObsClient obsClient = new ObsClient(ak, sk, endPoint);
//        obsClient.setBucketAcl(bucketName, AccessControlList.REST_CANNED_PUBLIC_READ_WRITE_DELIVERED);

//        AccessControlList acl = obsClient.getBucketAcl(bucketName);
//        log.info("GetBucketAcl: {}", acl);

        AccessControlList acl = new AccessControlList();
        acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_READ);
        obsClient.setBucketAcl(bucketName, acl);
        PutObjectResult putObjectResult = obsClient.putObject(bucketName, "daxiong2.jpg", new File("/Users/xiangnan/Pictures/daxiong.jpg"));

//        ObsObject obsObject = obsClient.getObject(bucketName, "daxiong2.jpg");
//
//        // 读取对象内容
//        System.out.println("Object content:");
//        InputStream input = obsObject.getObjectContent();
//        byte[] b = new byte[1024];
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        int len;
//        while ((len=input.read(b)) != -1){
//            bos.write(b, 0, len);
//        }
//
//        System.out.println(new String(bos.toByteArray()));
//        bos.close();
//        input.close();

        UacAccount uacAccount = new UacAccount();
        uacAccount.setId(1L);
        uacAccount.setPassword("123123");
        uacAccount.setRole("role1");

        UacAccount uacAccount2 = new UacAccount();
        uacAccount2.setId(1L);
        uacAccount2.setPassword("123123");
        uacAccount2.setRole("role1");
    }
}
